<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="预警标题" prop="title">
        <el-input v-model="queryParams.title" placeholder="请输入预警标题" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="是否处理" prop="isHandleDescribe">
          <el-select v-model="queryParams.isHandle" placeholder="是否处理">
            <el-option v-for="item in this.warningIsHandle" :key="item.code"
              :label="item.message"
              :value="item.code"></el-option>
          </el-select>

      </el-form-item>
      <el-form-item label="灾害名称" prop="emName">
        <el-input v-model="queryParams.emName" placeholder="请输入灾害名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="warningList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="预警标题" align="center" prop="title" />
      <el-table-column label="预警内容" align="center" prop="context" />
      <el-table-column label="图片" align="center" prop="image" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.image" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="受伤人员数量" align="center" prop="hurt" />
      <el-table-column label="死亡人员数量" align="center" prop="death" />
      <el-table-column label="损失情况" align="center" prop="wastage" />
      <el-table-column label="预警处理情况" align="center" prop="treatment" />
      <el-table-column label="是否处理" align="center" prop="isHandleDescribe" />
      <el-table-column label="灾害名称" align="center" prop="emName" />
      <el-table-column label="灾害类别" align="center" prop="emType" />
      <el-table-column label="应对处理方法" align="center" prop="handleMethod" />
      <el-table-column label="预警创建时间" align="center" prop="createTime" />
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改预警信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="预警标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入预警标题" />
        </el-form-item>
        <el-form-item label="预警内容" prop="context">
          <el-input v-model="form.context" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="预警处理情况" prop="treatment">
          <el-input v-model="form.treatment" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="是否处理" prop="isHandleDescribe">
          <!-- <el-input v-model="form.isHandle" placeholder="请输入是否处理  0 - 未处理   1  处理中   2 处理完成" /> -->
          <el-select v-model="form.isHandle" placeholder="是否处理">
            <el-option v-for="item in this.warningIsHandle" :key="item.code"
              :label="item.message"
              :value="item.code"></el-option>
          </el-select>

        </el-form-item>
        <el-form-item label="灾害名称" prop="emName">
          <el-input v-model="form.emName" placeholder="请输入类别名称" />
        </el-form-item>
        <el-form-item label="图片" prop="image">
          <image-upload v-model="form.image" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 发布预警 -->
    <el-dialog :title="title" :visible.sync="sendWaringView" width="500px" append-to-body>
      <el-form ref="warningForm" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="预警人员" prop="title">
          <el-select v-model="form.users" multiple placeholder="请选择协助预警人员">
            <el-option v-for="item in this.warningUserInfoList" :key="item.userId"
              :label="'人员名称:' + item.nickName + '  岗位:' + JSON.stringify(item.dept.deptName)"
              :value="item.userId"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitwarningForm">推送预警信息</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 通知人员 -->
    <el-dialog :title="title" :visible.sync="noticeView" width="600px" append-to-body>
      <el-table v-loading="loading" :data="noticeUserInfo">
        <el-table-column label="名称" align="center" prop="nickName" />
        <el-table-column label="手机号码" align="center" prop="phonenumber" />
        <el-table-column label="部门" align="center" prop="dept.deptName" />
        <el-table-column label="是否通知" align="center" prop="isNoticeDescribe" />

        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button size="primary" type="text" @click="handleNoticeCall(scope.row)" 
            v-if="scope.row.isNotice == 0"
              >通知人员</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import { listWarning, getWarning, delWarning, addWarning, updateWarning, pullUserInfo, getWarningUserInfo ,callUser} from "@/api/system/warning";
import { listUser } from "@/api/system/user";

export default {
  name: "Warning",
  data() {
    return {
      warningIsHandle:[
        {'code':0,'message':"未处理"},
        {'code':1,'message':"处理中"},
        {'code':2,'message':"处理完成"}
      ],
      callUserInfo: {},
      noticeUserInfo: [],
      noticeView: false,
      warningUserInfoList: [],
      pullUserInfoData: {},
      sendWaringView: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 预警信息表格数据
      warningList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        treatment: null,
        isHandle: null,
        emName: null,
        emType: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询预警信息列表 */
    getList() {
      this.loading = true;
      listWarning(this.queryParams).then(response => {
        this.warningList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.sendWaringView = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        context: null,
        casualties: null,
        wastage: null,
        treatment: null,
        isHandle: null,
        emTypeId: null,
        emName: null,
        emType: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加预警信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getWarning(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改预警信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateWarning(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWarning(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    submitwarningForm() {
      this.$refs["warningForm"].validate(valid => {
        if (valid) {
          this.pullUserInfoData.users = this.form.users;
          this.pullUserInfoData.warningId = localStorage.getItem("id")
          pullUserInfo(this.pullUserInfoData).then(response => {
            this.pullUserInfoData = {};
            this.$modal.msgSuccess("信息推送成功");
            this.sendWaringView = false;
            this.getList();
          })
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除预警信息编号为"' + ids + '"的数据项？').then(function () {
        return delWarning(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/warning/export', {
        ...this.queryParams
      }, `warning_${new Date().getTime()}.xlsx`)
    },
    handleSendWaringInfo(row) {
      this.sendWaringView = true;
      this.title = "发布预警信息";
      listUser().then(response => {
        this.warningUserInfoList = response.rows;
      })
      // console.log( "用户信息" + JSON.stringify(this.warningUserInfoList))
      localStorage.setItem("id", row.id);
    },
    handleNotice(row) {
      this.noticeView = true;
      var id = row.id;
      this.title = "预警人员信息-----------请通过手机号码通知"
      localStorage.setItem("id", id)
      getWarningUserInfo(id).then(response => {
        this.noticeUserInfo = response.data;
      })
    },
    handleNoticeCall(row) {
      this.callUserInfo.id = localStorage.getItem("id");
      this.callUserInfo.userId = row.userId
      callUser(this.callUserInfo).then(response => {
        this.$modal.msgSuccess("呼叫成功");
        // this.noticeView = false;
        this.callUserInfo = {}
        getWarningUserInfo(localStorage.getItem("id")).then(response => {
        this.noticeUserInfo = response.data;
      })
      })
    }
  }
};
</script>
